/**
 * @Author suoyue_zhan
 * @Date 2021/1/25 07:16
 * @Description
 */
public class TryCatchFinally {

	public static void main(String[] args) {
		int i = 12;
		System.out.println("i: "+i+"\t注意观察finally中的i及return的执行");
		System.out.println("tryMethod1: "+tryMethod1(i));
		System.out.println("tryMethod2: "+tryMethod2(i));
	}

	//finally会执行，i值会修改，try中return的i值不会被修改,也不会执行，因为finally中有return覆盖了try中的return
	private static int tryMethod2(int i) {
		try {
			i++;
			System.out.println("try:"+i);
			return i;
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			i--;
			System.out.println("finally:"+i);
			return i;
		}
	}

	//finally会执行，i值会修改，但try中return的i值不会被修改
	private static int tryMethod1(int i) {
		try {
			i++;
			System.out.println("try:"+i);
			return i;
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			i--;
			System.out.println("finally:"+i);
		}
		return i;
	}

}
